package com.yl.message.websocket

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.server.ServerHttpRequest
import org.springframework.messaging.converter.MessageConverter
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler
import org.springframework.messaging.simp.config.ChannelRegistration
import org.springframework.messaging.simp.config.MessageBrokerRegistry
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker
import org.springframework.web.socket.config.annotation.StompEndpointRegistry
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration
import org.springframework.web.socket.server.standard.ServerEndpointExporter
import org.springframework.web.socket.server.support.DefaultHandshakeHandler
import java.security.Principal

@Configuration
@EnableWebSocketMessageBroker
open class WebSocketConfig : WebSocketMessageBrokerConfigurer {
	@Bean
	open fun serverEndpointExporter(): ServerEndpointExporter {
		return ServerEndpointExporter()
	}

	override fun registerStompEndpoints(registry: StompEndpointRegistry) {
		registry.addEndpoint("/webscoket/server/**")
			.addInterceptors(HttpHandShakeInterceptor())
//			.setHandshakeHandler(object :DefaultHandshakeHandler(){
//
//				override fun  determineUser(request: ServerHttpRequest,
//				                            wsHandler: WebSocketHandler,
//				                            attributes:Map<String, Any> ): Principal {
//
//					return  Principal { attributes["userName"].toString() }
//				}
//			})
//			.setAllowedOrigins("*")
//			.withSockJS()
	}

	/**
	 * 配置消息代理(中介)
	 * enableSimpleBroker 服务端推送给客户端的路径前缀
	 * setApplicationDestinationPrefixes 客户端发送数据给服务器端的一个前缀
	 */
	override fun configureMessageBroker(registry: MessageBrokerRegistry){
//		registry.enableSimpleBroker("/topic", "/chat", "/check");
//		registry.setApplicationDestinationPrefixes("/app");
	}

	/**
	 * 消息传输参数配置
	 */
	override fun configureWebSocketTransport(registry: WebSocketTransportRegistration) {
		registry.setMessageSizeLimit(8192) //设置消息字节数大小
			.setSendBufferSizeLimit(8192)//设置消息缓存大小
			.setSendTimeLimit(10000); //设置消息发送时间限制毫秒
	}

	/**
	 * 配置客户端入站通道拦截器
	 */
	override fun configureClientInboundChannel(registration: ChannelRegistration) {
		registration.taskExecutor().corePoolSize(4) //设置消息输入通道的线程池线程数
			.maxPoolSize(8)//最大线程数
			.keepAliveSeconds(60);//线程最大空闲时间
		//前面配置的spring channel拦截器 过时不建议再使用
//		registration.interceptors(SocketChannelIntecepter());

	}
	/**
	 * 配置客户端出站通道拦截器
	 */
	override fun configureClientOutboundChannel(registration: ChannelRegistration) {
		registration.taskExecutor().corePoolSize(4).maxPoolSize(8);
		//前面配置的spring channel拦截器 过时不建议再使用
//		registration.interceptors(  SocketChannelIntecepter())

	}

	override fun addArgumentResolvers(argumentResolvers: MutableList<HandlerMethodArgumentResolver>) {
		super.addArgumentResolvers(argumentResolvers)
	}

	override fun addReturnValueHandlers(returnValueHandlers: MutableList<HandlerMethodReturnValueHandler>) {
		super.addReturnValueHandlers(returnValueHandlers)
	}

	override fun configureMessageConverters(messageConverters: MutableList<MessageConverter>): Boolean {
		return super.configureMessageConverters(messageConverters)
	}
}
